<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="90" label-position="right">
        <FormItem label="上级类别" prop="parent_id">            
            <RadioGroup v-model="formValidate.parent_id">
                <Radio :label="1">团队所属</Radio>
                <Radio :label="2">团队项目</Radio> 
                <Radio :label="3">单项项目</Radio>
                <Radio :label="4">二维码项目</Radio>
                <Radio :label="5">费用项目</Radio>
                <Radio :label="6">旅游特料</Radio>              
            </RadioGroup>             
        </FormItem>
        <FormItem label="类别名称" prop="title">
            <Input type="text" v-model="formValidate.title"></Input>
        </FormItem>               
        
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">确定</Button>
        </FormItem>
    </Form>
</template>
<script>

export default {
    name: 'categoryinfo',
    props: ['formDate'],
    data(){
        return {  
            formValidate: this.formDate,            
            ruleValidate:{
                title:[
                    { required: true, message: '请输入类别名称', trigger: 'blur' }
                ],
                parent_id:[
                    { type:'number', required: true, message: '请输入选择上级分类', trigger: 'blur' }
                ]
            }       
        }
    },
    methods: { 
        addCategory(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/category/create',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('录入成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('录入失败!');
            });   
        },
        editCategory(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/category/update',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('编辑成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('编辑失败!');
            });   
        },                 
        handleSubmit(name){                 
            this.$refs[name].validate((valid) => {                   
                if (valid) {                                 
                    if(this.formValidate.id != ""){
                        this.editCategory(); 
                    }else{
                        this.addCategory();                                 
                    }  
                    this.$emit('updateCategoryList');    
                    this.$emit('closeDialog');              
                } else {
                    this.$Message.error('提交失败!');
                }
            })
        }
    }
}
</script>
